package day5.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.ByteToMessageCodec;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;

import java.util.Arrays;
import java.util.List;

public class ChannelInitHandler extends ChannelInitializer<NioSocketChannel> {

    final byte[] a = {0x11,0x12,0x13};

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ChannelInboundHandlerAdapter handlerAdapter = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                ctx.fireChannelRead(msg);
            }
        };
//        ByteToMessageCodec<ByteBuf> byteToMessageCodec = new ByteToMessageCodec<ByteBuf>() {
//            @Override
//            protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
//                out.writeBytes(msg);
//            }
//
//            @Override
//            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
////                int i1 = in.readableBytes();
////                in.markReaderIndex();
////                ByteBuf byteBuf = in.readBytes(i1);
////                byte[] bytes = ByteBufUtil.getBytes(byteBuf);
////                int i = byteBuf.readableBytes();
////                byte[] bytes1 = new byte[i];
////                byteBuf.readBytes(bytes1);
////                byteBuf.release();
////                in.resetReaderIndex();
//                out.add(in);
//            }
//
//        };
        ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
            @Override
            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
                out.add(in);
            }
        };
        MessageToByteEncoder<ByteBuf> encoder = new MessageToByteEncoder<ByteBuf>() {

            @Override
            protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
                out.writeBytes(msg);
            }
        };
        SimpleChannelInboundHandler<ByteBuf> simpleChannelInboundHandler = new SimpleChannelInboundHandler<ByteBuf>() {
            @Override
            protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
//                if (Arrays.equals(msg,new byte[]{0x11,0x12,0x13})) {
//                    System.out.println(Arrays.toString(msg));
//                    ctx.channel().writeAndFlush(new byte[]{0x12,0x13,0x14});
//                }
                int i = msg.readableBytes();
                byte[] bytes = new byte[i];
                msg.readBytes(bytes);
            }
        };
//        ch.pipeline().addLast(handlerAdapter);
        A a = new A();
//        ch.pipeline().addLast(decoder).addLast(encoder);
        ch.pipeline().addLast(a);
        ch.pipeline().addLast(simpleChannelInboundHandler);
    }

    class A extends ByteToMessageDecoder{

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            ctx.fireChannelRead(in);
//            out.add(in);
//            in.release();
        }
    }
}
